package com.eschool.beans;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javassist.expr.NewExpr;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import org.primefaces.component.graphicimage.GraphicImage;
import org.primefaces.model.DefaultTreeNode;
import org.primefaces.model.TreeNode;

import com.eschool.dao.SchoolDao;
import com.eschool.model.RechargeHistory;
import com.eschool.model.School;
import com.eschool.util.UserUtil;

@ManagedBean(name = "historyBean")
@ViewScoped
public class HistoryBean {

	private List rechargeHistory;
	private ArrayList<ArrayList<RechargeHistory>> listOflists; // for making separate list of each school's recharge history
	private TreeNode root;
	
	public HistoryBean() {
		root = new DefaultTreeNode("Root", null);
		listOflists = new ArrayList<ArrayList<RechargeHistory>>();
		rechargeHistory = SchoolDao.getRechargeHistory();
		populateListOfLists();
		makeTree();
	}
	  
    public TreeNode getRoot() {  
        return root;  
    }  
	
	private void populateListOfLists() {
		int previousSchoolId=-1;
		int newSchoolId=-2;
		int index=0;
		
		if (rechargeHistory != Collections.EMPTY_LIST) {
			RechargeHistory firstRechargeHistory = (RechargeHistory) rechargeHistory.get(0);
			previousSchoolId = firstRechargeHistory.getSchool().getSchoolId();
			listOflists.add(new ArrayList<RechargeHistory>());
			listOflists.get(index).add(firstRechargeHistory);
		
			// Iterating the list from 1 becuase we have already added the first element in listOflists
			for (int i=1; i<rechargeHistory.size() ; i++) {
				RechargeHistory current = (RechargeHistory) rechargeHistory.get(i);
				newSchoolId = current.getSchool().getSchoolId();
				
				if (previousSchoolId == newSchoolId) {
					listOflists.get(index).add(current);				
				}
				
				else {
					index++;
					previousSchoolId = newSchoolId;
					listOflists.add(new ArrayList<RechargeHistory>());
					listOflists.get(index).add(current);
				}
			}
		}
	}
	
	private void makeTree() {
		
		for (Iterator iterator = listOflists.iterator(); iterator.hasNext();) {
			ArrayList<RechargeHistory> currentList = (ArrayList<RechargeHistory>) iterator.next();
			boolean first=true;
			TreeNode node0 = new DefaultTreeNode();
			TreeNode node00;
			
			for (Iterator iterator2 = currentList.iterator(); 
					iterator2.hasNext();) {
				RechargeHistory rechargeHistory = (RechargeHistory) iterator2.next();
				
				if (first == true) {
					node0 = new DefaultTreeNode(rechargeHistory.getSchool().getSchoolName(), root);
					node00 = new DefaultTreeNode(
							getHistoryMessage(rechargeHistory.getSchool(),
									rechargeHistory.getRechargeDate(),
									rechargeHistory.getAdmin().getName())
							, node0);
					first = !first;
				}
				else {
					node00 = new DefaultTreeNode(
							getHistoryMessage(rechargeHistory.getSchool(),
									rechargeHistory.getRechargeDate(),
									rechargeHistory.getAdmin().getName())
							, node0);
				}
					
			}
		}
    } 
	
	private String getHistoryMessage(School school, Date rechargeDate, String adminName) {
		return "Recharged on " + rechargeDate + " by " + adminName;
	}

}
